<?php

namespace backend\models;

use Yii;
use Da\User\Model\User;

/**
 * This is the model class for table "project".
 *
 * @property int $id
 * @property string|null $title
 * @property string|null $description
 *
 * @property Contract[] $contracts
 */
class Project extends \yii\db\ActiveRecord
{    
    const STATUS_CLOSE = 0;
    const STATUS_PLANNED = 5;
    const STATUS_ACTIVE = 10;
    const STATUS_IDEA = 20;

    const PROXY = 1;
    const OWN = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'project';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['status', 'title'], 'required'],
            [['title'], 'string', 'max' => 255],
            [['url_work_budget'], 'string', 'max' => 512],
            [['description'], 'string'],
            [['status', 'id_accountant', 'id_curator', 'is_proxy'], 'integer'],
            [['date_start', 'date_realize'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Название',
            'description' => 'Описание',
            'status' => 'Статус',
            'date_start' => 'Дата старта',
            'date_realize' => 'Дата завершения',
            'id_accountant' => 'Ответственный бухгалтер',
            'id_curator' => 'Куратор',
            'url_work_budget' => 'Ссылка на рабочую смету',
            'is_proxy' => 'Проект-прокси',
        ];
    }

    /**
     * Gets query for [[Contracts]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getContracts()
    {
        return $this->hasMany(Contract::className(), ['id_project' => 'id']);
    }

    public function getAccountant()
    {
        return $this->hasOne(User::className(), ['id' => 'id_accountant']);
    }

    public function getCurator()
    {
        return $this->hasOne(User::className(), ['id' => 'id_curator']);
    }

    public function getSmi()
    {
        return $this->hasMany(ProjectSmi::className(), ['id_project' => 'id']);
    }

    public function getDonations()
    {
        return $this->hasMany(ProjectDonation::className(), ['project_id' => 'id']);
    }

    public static function getStatusList()
    {
        return [
            self::STATUS_PLANNED => 'Планируемый',
            self::STATUS_ACTIVE => 'В работе',
            self::STATUS_CLOSE => 'Закрыт',
            self::STATUS_IDEA => 'Идея',
        ];
    }

}
